package trading.bo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
import com.amazonaws.services.simpleemail.model.Body;
import com.amazonaws.services.simpleemail.model.Content;
import com.amazonaws.services.simpleemail.model.Destination;
import com.amazonaws.services.simpleemail.model.Message;
import com.amazonaws.services.simpleemail.model.SendEmailRequest;

/**
 * Send email notification for various activities
 * 
 * @author root
 * 
 */
public class EmailAccessorBO {

    private AmazonSimpleEmailServiceClient client;
    private SendEmailRequest request;

    public void setAwsCredentialFileLocation(File awsCredentialFileLocation)
            throws FileNotFoundException, IllegalArgumentException, IOException {
        client = new AmazonSimpleEmailServiceClient(new PropertiesCredentials(
                awsCredentialFileLocation));
        init();
    }

    private void init() {
        request = new SendEmailRequest().withSource("rexjacobb@gmail.com");
        List<String> toAddresses = new ArrayList<String>();
        toAddresses.add("rexjacobb@gmail.com");
        Destination dest = new Destination().withToAddresses(toAddresses);
        request.setDestination(dest);
    }

    public void sendMktSentimentChangeEmail(String from, String to) {
        String subject = "Market sentiment changing notification";
        String content = "The market sentiment has changed from: " + from
                + " to " + to;
        sendEmailCommon(subject, content);
    }

    public void sendNewShortStockOrderEmail(String ticker, String size) {
        String subject = "New stock order has been submitted";
        String content = "Trading system has submitted an order to SELL "
                + ticker + " for " + size + " shares.";
        content += "/n The order type is MKT.";
        sendEmailCommon(subject, content);
    }
    
    public void sendNewStockBuyOrderEmail(String ticker, String size) {
        String subject = "New stock order has been submitted";
        String content = "Trading system has submitted an order to BUY " + ticker + " for " + size + " shares. The order type is MKT.";
        sendEmailCommon(subject, content);
    }

    public void sendStockTrailOrderEmail(String ticker, String size,
            String trailAmount) {
        String subject = "New stock order has been submitted";
        String content = "Trading system has submitted an order to BUY " + ticker + " for " + size + " shares.";
        content += "/n The order type is TRAIL. The trail amount is "
                + trailAmount;
        sendEmailCommon(subject, content);
    }

    public void sendStockTrailOrderUpdateEmail(String orderId, String ticker,
            String size, String oldTrailAmount, String newTrailAmount) {
        String subject = "Stock trail order has been updated";
        String content = "The stock trail order is updated. Order id : "
                + orderId + "; ticker : " + ticker + "; size : " + size
                + "; original trail amount : " + oldTrailAmount
                + "; new trail amount : " + newTrailAmount;
        sendEmailCommon(subject, content);
    }

    public void sendNewOptionOrderEmail(String action, String ticker,
            String type, String size, String expiry, String strikePrice,
            String cOrP) {
        String subject = "New option order has been submitted";
        String content = "Trading system has submitted a new option order. Order detail is : /n";
        content += "ticker : " + ticker;
        content += "/n expiry : " + expiry;
        content += "/n strikePrice : " + strikePrice;
        content += "/n action : " + action;
        content += "/n size : " + size;
        content += "/n type : " + type;
        content += "/n call or put : " + cOrP;
        sendEmailCommon(subject, content);
    }

    public void sendNewHistoricalLowEmail(String ticker, String price) {
        String subject = "New historical low detected";
        String content = "Stock " + ticker + " has hit new historical low : "
                + price;
        sendEmailCommon(subject, content);
    }

    public void sendStopPriceUpdateEmail() {
        String subject = "Stock price update is stopped";
        String content = "Either the market hour is passed, or the market sentiment is changed.";
        sendEmailCommon(subject, content);
    }
    
    public void sendCancelOrderEmail(int orderId) {
        String subject = "An order is cancelled";
        String content = "Order with order id : " + orderId + " is cancelled.";
        sendEmailCommon(subject, content);
    }

    private void sendEmailCommon(String subject, String content) {
        Content subjContent = new Content().withData(subject);
        Message msg = new Message().withSubject(subjContent);
        Content textContent = new Content().withData(content);
        Body body = new Body().withText(textContent);
        msg.setBody(body);
        request.setMessage(msg);
        try {
            client.sendEmail(request);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
